package org.example.ruoxing_admin_springboot3.utils;

import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import lombok.extern.slf4j.Slf4j;
import org.example.ruoxing_admin_springboot3.config.MinioPropertiesConfig;
import org.example.ruoxing_admin_springboot3.viewmodel.FileUploadResult;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

@Component
@Slf4j
public class MinioUtil {
    private static MinioClient minioClient = null;
    private static MinioPropertiesConfig minioPropertiesConfig = new MinioPropertiesConfig();

    public MinioUtil(MinioClient minioClient, MinioPropertiesConfig minioPropertiesConfig) {
        MinioUtil.minioClient = minioClient;
        MinioUtil.minioPropertiesConfig = minioPropertiesConfig;
    }

    /**
     * 上传文件到 MinIO
     *
     * @param file 文件
     * @return 文件URL
     */
    public static FileUploadResult uploadFile(MultipartFile file) {
        try (InputStream inputStream = file.getInputStream()) {
            String objectName = file.getOriginalFilename();
            minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(minioPropertiesConfig.getBucketName())
                            .object(objectName)
                            .stream(inputStream, file.getSize(), -1)
                            .contentType(file.getContentType())
                            .build()
            );
            log.info("File '{}' uploaded successfully.", objectName);
            return new FileUploadResult(objectName, minioPropertiesConfig.getEndpoint() + "/" + minioPropertiesConfig.getBucketName() + "/" + objectName);
        } catch (Exception e) {
            log.error("Error occurred while uploading file: {}", e.getMessage());
            throw new RuntimeException("File upload failed", e);
        }
    }

    /**
     * 获取文件从 MinIO
     *
     * @param objectName 对象名称
     * @return 输入流
     */
    public InputStream getFileStream(String objectName) {
        try {
            return minioClient.getObject(
                    GetObjectArgs.builder()
                            .bucket(minioPropertiesConfig.getBucketName())
                            .object(objectName)
                            .build()
            );
        } catch (Exception e) {
            log.error("Error occurred while downloading file: {}", e.getMessage());
            throw new RuntimeException("File download failed", e);
        }
    }

    /**
     * 删除文件从 MinIO
     *
     * @param objectName 对象名称
     */
    /**
     * 删除文件从 MinIO
     *
     * @param objectName 对象名称
     * @return 是否成功删除
     */
    public boolean deleteFile(String objectName) {
        try {
            minioClient.removeObject(
                    RemoveObjectArgs.builder()
                            .bucket(minioPropertiesConfig.getBucketName())
                            .object(objectName)
                            .build()
            );
            log.info("File '{}' deleted successfully.", objectName);
            return true;
        } catch (Exception e) {
            log.error("Error occurred while deleting file: {}", e.getMessage());
            return false;
        }
    }
}
